package zero.tech.games.log.hot;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import zero.tech.core.data.PageBean;
import zero.tech.games.common.RedisConsts;
import zero.tech.games.entity.log.LogGoldChange;
import zero.tech.games.log.mapper.LogGoldChangeMapper;
import zero.tech.redis.cache.RedisUtil;

@Component
public class LogGoldChangeUtil {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private LogGoldChangeMapper logGoldChangeMapper;
	@SuppressWarnings("unchecked")
	public PageBean<LogGoldChange> goldChangeLogs(int uid, int club_id, int page, int pageLength, Date begin,
			Date end) {
		PageBean<LogGoldChange> pageBean=(PageBean<LogGoldChange>) redisUtil.hget(RedisConsts.HOT_LOG_GOLDCHANGE_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logGoldChangeMapper.totalCount(uid,club_id,begin,end);
			int start=page*pageLength;
			List<LogGoldChange> list=logGoldChangeMapper.findLogGoldChangeByPage(uid,club_id,start,pageLength,begin,end);
			pageBean = new PageBean<>(totalCount, pageLength, page, list);
			redisUtil.hset(RedisConsts.HOT_LOG_GOLDCHANGE_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}
	@SuppressWarnings("unchecked")
	public PageBean<LogGoldChange> goldChangeLogsByChuru(int uid, int club_id, int page, int pageLength, Date begin,
			Date end) {
		PageBean<LogGoldChange> pageBean=(PageBean<LogGoldChange>) redisUtil.hget(RedisConsts.HOT_LOG_GOLDCHANGE_CHURU_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logGoldChangeMapper.totalCountByChuru(uid,club_id,begin,end);
			int start=page*pageLength;
			List<LogGoldChange> list=logGoldChangeMapper.findLogGoldChangeChuruByPage(uid,club_id,start,pageLength,begin,end);
			pageBean = new PageBean<>(totalCount, pageLength, page, list);
			redisUtil.hset(RedisConsts.HOT_LOG_GOLDCHANGE_CHURU_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}
	@SuppressWarnings("unchecked")
	public PageBean<LogGoldChange> goldChangeLogsByShuying(int uid, int club_id, int page, int pageLength, Date begin,
			Date end) {
		PageBean<LogGoldChange> pageBean=(PageBean<LogGoldChange>) redisUtil.hget(RedisConsts.HOT_LOG_GOLDCHANGE_SHUYING_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logGoldChangeMapper.totalCountByShuying(uid,club_id,begin,end);
			int start=page*pageLength;
			List<LogGoldChange> list=logGoldChangeMapper.findLogGoldChangeShuyingByPage(uid,club_id,start,pageLength,begin,end);
			pageBean = new PageBean<>(totalCount, pageLength, page, list);
			redisUtil.hset(RedisConsts.HOT_LOG_GOLDCHANGE_SHUYING_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}
	@SuppressWarnings("unchecked")
	public PageBean<LogGoldChange> goldChangeLogsByChayeku(int uid, int club_id, int page, int pageLength, Date begin,
			Date end) {
		PageBean<LogGoldChange> pageBean=(PageBean<LogGoldChange>) redisUtil.hget(RedisConsts.HOT_LOG_GOLDCHANGE_CHAYEKU_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logGoldChangeMapper.totalCountByChayeku(uid,club_id,begin,end);
			int start=page*pageLength;
			List<LogGoldChange> list=logGoldChangeMapper.findLogGoldChangeChayekuByPage(uid,club_id,start,pageLength,begin,end);
			pageBean = new PageBean<>(totalCount, pageLength, page, list);
			redisUtil.hset(RedisConsts.HOT_LOG_GOLDCHANGE_CHAYEKU_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}
	public void insertBatch(List<LogGoldChange> logGoldChanges) {
		logGoldChangeMapper.insertBatch(logGoldChanges);
		List<String> list=new ArrayList<String>();
		for(LogGoldChange logGoldChange:logGoldChanges) {
			int club_id=logGoldChange.getClub_id();
			int uid=logGoldChange.getUid();
			String str=club_id+"_"+uid;
			if(!list.contains(str)) {
				list.add(str);
			}
		}
		for(String str:list) {
			redisUtil.del(RedisConsts.HOT_LOG_GOLDCHANGE_PAGE+"_"+str);
			redisUtil.del(RedisConsts.HOT_LOG_GOLDCHANGE_CHURU_PAGE+"_"+str);
			redisUtil.del(RedisConsts.HOT_LOG_GOLDCHANGE_SHUYING_PAGE+"_"+str);
			redisUtil.del(RedisConsts.HOT_LOG_GOLDCHANGE_CHAYEKU_PAGE+"_"+str);
		}
	}
}
